package backtracking.combination;

import java.util.LinkedList;
import java.util.List;

/**
 * 17. 电话号码的字母组合
 * <p>
 * 给定一个仅包含数字2-9的字符串，返回所有它能表示的字母组合。答案可以按 任意顺序 返回。
 * <p>
 * 给出数字到字母的映射如下（与电话按键相同）。注意 1 不对应任何字母。
 */
public class N17_LetterCombinationsOfAPhoneNumber {
    private List<String> res = new LinkedList<>();
    private StringBuilder sb = new StringBuilder();
    private String[] letterMap = {
            "",
            "",
            "abc",
            "def",
            "ghi",
            "jkl",
            "mno",
            "pqrs",
            "tuv",
            "wxyz"
    };

    private void backtracking(String digits, int index) {
        if (index == digits.length()) {
            res.add(sb.toString());
            return;
        }
        int digit = digits.charAt(index) - '0';
        String letters = letterMap[digit];
        for (int i = 0; i < letters.length(); i++) {
            sb.append(letters.charAt(i));
            backtracking(digits, index + 1);
            sb.deleteCharAt(i);
        }
    }

    public List<String> letterCombinations(String digits) {
        if (digits.length() == 0) return res;
        backtracking(digits, 0);
        return res;
    }
}
